<template>
<!-- 新增公告弹框 -->
  <div>
    <el-dialog title="新增公告" :visible.sync="dialogVisible" :before-close="close" :close-on-click-modal="false" width="60%">
    <el-form :rules="noticeRules" :model="noticeForm" ref="noticeRef" hide-required-asterisk>
        <el-form-item label="公告内容:" prop="notice">
          <el-input type="textarea" v-model="noticeForm.notice" :autosize="{minRows: 3}" 
          maxlength="150" placeholder="请填写公告内容" show-word-limit></el-input>
       </el-form-item>     
    </el-form>
     <span slot="footer" class="dialog-footer">
        <el-button @click="close()" :loading="buttonLoading">取 消</el-button>
        <el-button type="primary" @click="add()" :loading="buttonLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
 data() {
    return {
      dialogVisible: false,
      buttonLoading: false,
      noticeForm:{
        nickname:'',
        username:'',
        notice:'',
        createTime: '',
      },
      noticeRules: {
        notice: [{ required: true, message: "请填写公告内容", trigger: "blur" }],
      }
    };
  },
  methods:{
    //打开
    open() {
      this.dialogVisible = true;
    },
    //关闭
    close() {
      this.dialogVisible = false;
      this.$refs.noticeRef.resetFields();
      this.noticeForm = {}
    },
    //新增
    add() {
      this.$refs.noticeRef.validate( async valid => {
        if (!valid) return 
        this.buttonLoading = true;
        this.noticeForm.createTime = Number(new Date())
        this.noticeForm.nickname = window.sessionStorage.nickname
        this.noticeForm.username = window.sessionStorage.username
        await this.$http.post("notice/addNotice",this.noticeForm)
        .then((res) => {
          console.log(res)
          if(res.status === 200){
            this.close();
            this.$message.success("新增公告成功!");
            this.$emit("getListData");
          }     
          this.buttonLoading = false;
        })
        .catch((err) => { 
          this.buttonLoading = false;
          this.$message.error("新增失败!")
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>